package Q819;

import java.util.HashMap;
import java.util.Map;

public class Main {

    public static void main(String[] args) {
        String string = "Bob hit a ball, the hit BALL flew far after it was hit.";
        String[] s = new String[]{"hit"};
        System.out.println(new Solution2().mostCommonWord(string, s));
    }
}

//最常见的单词
class Solution3 {
    public String mostCommonWord(String paragraph, String[] banned) {
        paragraph = paragraph.toLowerCase();
        paragraph = paragraph.replace('!', ' ').replace('?', ' ').replace('\'', ' ').replace(',', ' ').replace(';', ' ').replace('.', ' ').trim();
        String[] s = paragraph.split(" ");

        Map<String, Integer> map1 = new HashMap<>();
        Map<String, Integer> map2 = new HashMap<>();

        for (String str : banned) {
            map1.put(str, 1);
        }

        for (String s1 : s) {
            if (!map1.containsKey(s1) && !s1.equals("")) {
                map2.put(s1, map2.getOrDefault(s1, 0) + 1);
            }
        }

        int max = -1;
        String res = null;

        for (String s1 : map2.keySet()) {
            if (map2.get(s1) > max) {
                max = map2.get(s1);
                res = s1;
            }
        }
        return res;
    }
}
